package com.hluck.goodweatherapp.utils;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.hluck.goodweatherapp.BR;
import com.hluck.goodweatherapp.R;
import com.hluck.goodweatherapp.adapter.AdministrativeClickCallback;
import com.hluck.goodweatherapp.adapter.AreaAdapter;
import com.hluck.goodweatherapp.adapter.CityAdapter;
import com.hluck.goodweatherapp.adapter.ProvinceAdapter;
import com.hluck.goodweatherapp.data.bean.Province;
import com.hluck.goodweatherapp.databinding.DialogCityBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author： LJH
 * @Time： 2024/1/4
 * @description：
 */
public class CityDialog implements AdministrativeClickCallback {

    private static volatile CityDialog mInstance;
    private static Context mContext;
    private BottomSheetDialog dialog;
    private DialogCityBinding dialogCityBinding;

    private final List<Province> provinceList = new ArrayList<>();
    private final List<Province.City> cityList = new ArrayList<>();
    private final List<Province.City.Area> areaList = new ArrayList<>();

    private final CityAdapter cityAdapter = new CityAdapter(cityList);
    private final AreaAdapter areaAdapter = new AreaAdapter(areaList);

    private SelectedCityCallback selectedCityCallback;

    private String provinceName,cityName,areaName;

    public CityDialog(Context context, List<Province> provinces) {
        mContext = context;
        provinceList.clear();
        provinceList.addAll(provinces);
    }

    public static CityDialog getInstance(Context context,List<Province> provinces){
        if (mInstance == null){
            synchronized (CityDialog.class){
                if (mInstance == null){
                    mInstance = new CityDialog(context, provinces);
                }
            }
        }
        return mInstance;
    }

    /**
     * 设置选中城市回调
     * @param selectedCityCallback
     */
    public void setSelectedCityCallback(SelectedCityCallback selectedCityCallback) {
        this.selectedCityCallback = selectedCityCallback;
    }

    public void show(){
        dialog = new BottomSheetDialog(mContext);
        dialogCityBinding = DialogCityBinding.inflate(LayoutInflater.from(mContext),null,false);
        ProvinceAdapter provinceAdapter = new ProvinceAdapter(provinceList);
        provinceAdapter.setAdministrativeClickCallback(this);
        cityAdapter.setAdministrativeClickCallback(this);
        areaAdapter.setAdministrativeClickCallback(this);

        dialogCityBinding.rvProvince.setLayoutManager(new LinearLayoutManager(mContext));
        dialogCityBinding.rvProvince.setAdapter(provinceAdapter);

        dialogCityBinding.rvCity.setLayoutManager(new LinearLayoutManager(mContext));
        dialogCityBinding.rvCity.setAdapter(cityAdapter);

        dialogCityBinding.rvArea.setLayoutManager(new LinearLayoutManager(mContext));
        dialogCityBinding.rvArea.setAdapter(areaAdapter);

        dialogCityBinding.tvTitle.setOnClickListener(v -> {
            Log.d("TAG","tvTitle clicked");
        });
        dialogCityBinding.ivClose.setOnClickListener(v -> dialog.dismiss());
        dialogCityBinding.ivSubmit.setOnClickListener(v -> {
            Log.d("TAG", "ivSubmit clicked!"+(selectedCityCallback != null));
            if (selectedCityCallback != null){
                selectedCityCallback.selectedCity(areaName);
                dialog.dismiss();
            }
        });
        dialog.setContentView(dialogCityBinding.getRoot());
        dialog.show();
    }

    @Override
    public void onAdministrativeItemClick(View view, int position, AdministrativeType type) {
        switch (type){
            case PROVINCE:
                cityList.clear();
                cityList.addAll(provinceList.get(position).getCityList());
                cityAdapter.notifyDataSetChanged();
                dialogCityBinding.rvCity.setVisibility(View.VISIBLE);
                dialogCityBinding.rvArea.setVisibility(View.GONE);
                provinceName = provinceList.get(position).getProvinceName();
                cityName = null;
                areaName = null;
                break;
            case CITY:
                areaList.clear();
                areaList.addAll(cityList.get(position).getAreaList());
                areaAdapter.notifyDataSetChanged();
                dialogCityBinding.rvArea.setVisibility(View.VISIBLE);
                cityName = cityList.get(position).getCityName();
                areaName = null;
                break;
            case AREA:
                areaName = areaList.get(position).getAreaName();
                Log.d("TAG", "Area: " + areaName);
                break;
        }

        dialogCityBinding.ivSubmit.setVisibility(areaName == null ? View.INVISIBLE : View.VISIBLE);
        dialogCityBinding.tvTitle.setText(provinceName);
        if (cityName == null) return;
        dialogCityBinding.tvTitle.setText(provinceName + " > " + cityName);
        if (areaName == null) return;
        dialogCityBinding.tvTitle.setText(provinceName + " > " + cityName + " > " + areaName);
    }


    public void dismiss(){
        if (dialog == null){
            dialog.dismiss();
        }
    }

    public interface SelectedCityCallback{
        void selectedCity(String cityName);
    }

}
